package com.imagination.applet.domain.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.imagination.applet.controller.api.request.SaveHeadUrlReq;
import com.imagination.applet.controller.web.configuration.UserHolder;
import com.imagination.applet.domain.user.model.User;
import com.imagination.applet.domain.user.repository.UserRepository;
import com.imagination.applet.domain.user.service.UserService;
import com.imagination.applet.domain.utils.OssUtil;
import com.imagination.applet.domain.virtualidentity.model.UserVirtualIdentity;
import com.imagination.applet.domain.virtualidentity.service.UserVirtualIdentityCommandService;
import com.imagination.applet.infrastructure.handler.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;
    private final UserVirtualIdentityCommandService identity;

    @Override
    public Optional<User> getUserByOpenId(String openId) {
        return userRepository.findByOpenId(openId);
    }

    @Override
    public Optional<User> getByUserId(Long userId) {
        return userRepository.findById(userId);
    }

    @Override
    public User createUser(String openId, String openIdType, String shareCode) {
        User u = new User();
        u.setOpenId(openId);
        u.setOpenIdType(openIdType);
        u.setVirtualIdentityQtyThreshold(1);
        u.setStarCount(2);
        if(StrUtil.isNotBlank(shareCode)){
            u.setParentId(Long.parseLong(shareCode));
        }
        return userRepository.save(u);
    }

    @Override
    public void saveHeadUrl(SaveHeadUrlReq req) {
        Long userId = UserHolder.getUserId();
        if(userId != null){
            Optional<User> user = userRepository.findById(userId);
            if(user.isPresent()){
                User userSave = user.get();
                if(!StringUtils.isEmpty(req.getHeadUrl())){
                    userSave.setHeadUrl(req.getHeadUrl());
                }
                if(!StringUtils.isEmpty(req.getNickName())){
                    userSave.setNickName(req.getNickName());
                }
                userRepository.saveAndFlush(userSave);
            }
        }
    }

    @Override
    public void bindParent(Long parentId) {
        Long userId = UserHolder.getUserId();
        if(userId != null){
            Optional<User> user = userRepository.findById(userId);
            if(user.isPresent()){
                User userSave = user.get();
                if(userSave.getParentId()!=null){
                    return;
                }
                userSave.setParentId(parentId);
                userRepository.saveAndFlush(userSave);
            }
        }
    }

    @Override
    public void receiveciyuanbi(Long userId, String source) {
        Optional<User> user = userRepository.findById(userId);
        if(user.isPresent()){
            User userSave = user.get();
            if(source.equals("LZJ")){
                if (userSave.getLzj().equals("1")){
                    throw new ServiceException("已经领取过无法重复领取");
                }
                userSave.setLzj("1");
            }
            if(source.equals("XHYKF")){
                if (userSave.getXhykf().equals("1")){
                    throw new ServiceException("已经领取过无法重复领取");
                }
                userSave.setXhykf("1");
            }
            userSave.setStarCount(userSave.getStarCount()+5);
            userRepository.saveAndFlush(userSave);
            return;
        }
        throw new ServiceException("请登录");

    }

    @Override
    public JSONObject info() {
        Long userId = UserHolder.getUserId();

        JSONObject data = new JSONObject();
        Optional<User> user = userRepository.findById(userId);
        if(user.isPresent()){
            User userInfo = user.get();
            data.put("user", userInfo);
        }
        return data;
    }


}
